package flc.ast.fragment;

import android.os.Bundle;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.AlbumActivity;
import flc.ast.activity.ChangePwdActivity;
import flc.ast.activity.SetPwdActivity;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.bean.AlbumBean;
import flc.ast.databinding.FragmentAlbumBinding;
import flc.ast.dialog.AddDialog;
import flc.ast.utils.Content;
import flc.ast.view.PasscodeView;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AlbumFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AlbumFragment extends BaseNoModelFragment<FragmentAlbumBinding> {

    private AlbumAdapter albumAdapter;
    private List<AlbumBean> listShow = new ArrayList<>();//全部数据
    private List<String> listSel = new ArrayList<>();
    private boolean isEdit;
    private boolean isCheck;

    @Override
    protected int onCreate() {
        return R.layout.fragment_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.tvCancelEdit.setOnClickListener(this);
        mDataBinding.ivAlbumEdit.setOnClickListener(this);
        mDataBinding.ivAlbumAdd.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);

        mDataBinding.rvAlbumList.setLayoutManager(new GridLayoutManager(mContext, 3));
        albumAdapter = new AlbumAdapter();
        mDataBinding.rvAlbumList.setAdapter(albumAdapter);
        albumAdapter.setOnItemClickListener(this);

        mDataBinding.passcodeView.setListener(new PasscodeView.PasscodeViewListener() {
            @Override
            public void onFail() {
                mDataBinding.passcodeView.getWrongStatusColor();
            }

            @Override
            public void onSuccess(String number) {
                mDataBinding.rlAlbum.setVisibility(View.VISIBLE);
                mDataBinding.llInputPwd.setVisibility(View.GONE);
                isCheck = true;
                getData();
            }
        });
        mDataBinding.passcodeView.setMyClickListener(new PasscodeView.MyClickListener() {
            @Override
            public void click() {
                startActivity(ChangePwdActivity.class);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivAlbumEdit) {//编辑
            isEdit = true;
            albumAdapter.setEdit(isEdit);
            mDataBinding.tvCancelEdit.setVisibility(View.VISIBLE);
            mDataBinding.tvDelete.setText(getString(R.string.delete_num, listSel.size()));
            mDataBinding.tvDelete.setVisibility(View.VISIBLE);
        } else if (id == R.id.ivAlbumAdd) {//添加
            boolean isHave = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
            if (isHave) {
                showAddDialog();
            } else {
                startActivity(SetPwdActivity.class);
            }
        } else if (id == R.id.tvCancelEdit) {//取消编辑
            isEdit = false;
            listSel.clear();
            mDataBinding.tvCancelEdit.setVisibility(View.GONE);
            mDataBinding.tvDelete.setVisibility(View.GONE);
            albumAdapter.setEdit(isEdit);
        } else if (id == R.id.tvDelete) {//删除
            for (String path : listSel) {
                FileUtils.delete(path);
            }
            ToastUtils.showShort(getString(R.string.delete_suc));
            getData();
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (albumAdapter.getItem(position).isSelected()) {
                albumAdapter.getItem(position).setSelected(false);
                listSel.remove(albumAdapter.getItem(position).getPath());
            } else {
                albumAdapter.getItem(position).setSelected(true);
                listSel.add(albumAdapter.getItem(position).getPath());
            }
            albumAdapter.notifyItemChanged(position);
            mDataBinding.tvDelete.setText(getString(R.string.delete_num, listSel.size()));
        } else {
            AlbumActivity.sFilePath = FileUtils.getFileName(albumAdapter.getItem(position).getPath());
            startActivity(AlbumActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        boolean isHave = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
        if (isHave && !isCheck) {
            String password = SPUtil.getString(mContext, Content.myPassword, "");
            mDataBinding.passcodeView.setLocalPasscode(password);
            mDataBinding.rlAlbum.setVisibility(View.GONE);
            mDataBinding.llInputPwd.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rlAlbum.setVisibility(View.VISIBLE);
            mDataBinding.llInputPwd.setVisibility(View.GONE);
            getData();
        }
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myAlbum;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new AlbumBean(file.getPath()));
            }
        }
        if (listShow.size() > 0) {
            albumAdapter.setList(listShow);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.tvAlbumNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.tvAlbumNoData.setVisibility(View.VISIBLE);
        }
    }

    private void showAddDialog() {
        AddDialog dialog = new AddDialog(mContext);
        dialog.setListener(new AddDialog.Ilistener() {
            @Override
            public void clickAddAlbum(String name) {
                String path = PathUtils.getFilesPathExternalFirst() + Content.myAlbum + "/" + name;
                if (FileUtils.isFileExists(path)) {
                    ToastUtils.showShort(getString(R.string.album_is_exist));
                } else {
                    FileUtils.createOrExistsDir(path);
                    ToastUtils.showShort(getString(R.string.add_album_suc));
                }
                getData();
            }
        });
        dialog.show();
    }

}